<template>
  <div class="article-meta">
    <nuxt-link
      :to="{
        name: 'profile',
        params: {
          username: article.author.username,
        },
      }"
    >
      <img :src="article.author.image" />
    </nuxt-link>
    <div class="info">
      <nuxt-link
        class="author"
        :to="{
          name: 'profile',
          params: {
            username: article.author.username,
          },
        }"
      >{{ article.author.username }}</nuxt-link>
      <span class="date">{{ article.createAt | date("MMM DD, YYYY") }}</span>
    </div>
    <template v-if="article.author.username !== this.$store.state.user.username">
      <button
        class="btn btn-sm btn-outline-secondary"
        :class="{ active: article.author.following }"
        :disabled="article.followDisabled"
        @click="onFollow"
      >
        <i class="ion-plus-round"></i>
        &nbsp; {{article.author.following ? 'Unfollow' : 'Follow'}} {{ article.author.username }}
      </button>
      &nbsp;&nbsp;
      <button
        class="btn btn-sm btn-outline-primary"
        :class="{ active: article.favorited }"
        :disabled="article.favoriteDisabled"
        @click="onFavorite"
      >
        <i class="ion-heart"></i>
        &nbsp; Favorite Post
        <span class="counter">{{ article.favoritesCount }}</span>
      </button>
    </template>
    <template v-else>
      <nuxt-link
        class="btn btn-sm btn-outline-secondary"
        :to="{
        name: 'edit',
        params: {
          slug: article.slug
        }
      }"
      >
        <i class="ion-edit"></i>
        &nbsp; Edit Article
      </nuxt-link>&nbsp;&nbsp;
      <button
        class="btn btn-sm btn-outline-danger"
        :class="{ active: article.favorited }"
        @click="delArticle"
      >
        <i class="ion-trash-a"></i>
        &nbsp; Delete Article
      </button>
    </template>
  </div>
</template>

<script>
import { addFavorite, delFavorite } from "@/api/home";
import { follow, unFollow, delArticle } from "@/api/article";
export default {
  name: "ArticleMeta",
  head() {
    return {
      title: `${this.article.title} - RealWorld`,
      meta: [
        {
          // hid 是meta的唯一标识 防止name重复的meta
          hid: "description",
          name: "description",
          content: this.article.description
        }
      ]
    };
  },
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  methods: {
    async onFavorite() {
      this.article.favoriteDisabled = true;
      if (this.article.favorited) {
        await delFavorite(this.article.slug);
        this.article.favorited = false;
        this.article.favoritesCount -= 1;
      } else {
        await addFavorite(this.article.slug);
        this.article.favorited = true;
        this.article.favoritesCount += 1;
      }
      this.article.favoriteDisabled = false;
    },
    async onFollow() {
      this.article.followDisabled = true;
      if (this.article.author.following) {
        await unFollow(this.article.author.username);
        this.article.author.following = false;
      } else {
        await follow(this.article.author.username);
        this.article.author.following = true;
      }
      this.article.followDisabled = false;
    },
    async delArticle() {
      await delArticle(this.article.slug);
      this.$router.push("/");
    }
  }
};
</script>